package com.android.task5;


import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.Projection;

/** Overlay. Main function: Draw a circle on the map representing the current position from GPS. */

public class CustomOverlay extends Overlay {
 
	final int CIRCLERADIUS = 3;
 
	private GeoPoint geopoint;
	private GeoPoint north;
	private GeoPoint south;
	private GeoPoint west;
	private GeoPoint east;
	
	boolean hasBoarders = false;
 
	public CustomOverlay(GeoPoint point) {
		this.geopoint = point;
	}
	
	 /** Only used for testing of the boarder coordinates */
	public CustomOverlay(GeoPoint point,GeoPoint north,GeoPoint south,GeoPoint west,GeoPoint east) {
		this.geopoint = point;
		this.north = north;
		this.south = south;
		this.west = west;
		this.east = east;
		hasBoarders = true;
	}
	
	 /** Method that draws the circle representing the GPS position */
	@Override
	public void draw(Canvas canvas, MapView mapView, boolean shadow) {
	
		// Only for testing
		if(hasBoarders){
			
			Projection northProjection = mapView.getProjection();
			Point northPoint = new Point();
			northProjection.toPixels(this.north, northPoint);

			Projection southProjection = mapView.getProjection();
			Point southPoint = new Point();
			southProjection.toPixels(this.south, southPoint);

			Projection westProjection = mapView.getProjection();
			Point westPoint = new Point();
			westProjection.toPixels(this.west, westPoint);

			Projection eastProjection = mapView.getProjection();
			Point eastPoint = new Point();
			eastProjection.toPixels(this.east, eastPoint);
			

			// the circle to mark the spot
			Paint northCircle = new Paint();
			northCircle.setColor(Color.BLUE);
			northCircle.setAntiAlias(true);
			Paint southCircle = new Paint();
			southCircle.setColor(Color.RED);
			southCircle.setAntiAlias(true);
			Paint westCircle = new Paint();
			westCircle.setColor(Color.GREEN);
			westCircle.setAntiAlias(true);
			Paint eastCircle = new Paint();
			eastCircle.setColor(Color.YELLOW);
			eastCircle.setAntiAlias(true);

			// Draw the circle
			canvas.drawCircle(northPoint.x, northPoint.y, CIRCLERADIUS, northCircle);
			canvas.drawCircle(southPoint.x, southPoint.y, CIRCLERADIUS, southCircle);
			canvas.drawCircle(westPoint.x, westPoint.y, CIRCLERADIUS, westCircle);
			canvas.drawCircle(eastPoint.x, eastPoint.y, CIRCLERADIUS, eastCircle);
			

		}
		else{
			if(geopoint != null){
				// Transfrom geoposition to Point on canvas
				Projection projection = mapView.getProjection();
				Point point = new Point();
				projection.toPixels(geopoint, point);

				// the circle to mark the spot
				Paint circle = new Paint();
				circle.setColor(Color.BLUE);
				circle.setAntiAlias(true);

				// Draw the circle
				canvas.drawCircle(point.x, point.y, CIRCLERADIUS, circle);
			}

		}

	}
}